/**
 * all storeApi Control
 * @namespace StoreApi
 * @example 使用示例
 //hook 使用示例
 const [name1, setName1] = React.useState('hello');
 let cleanUserNameHook = StoreApi.addStoreStateHook('userName', setName1); //this will return the clean method for unSubcribe，返回值为取消本次hook的方法，需要时执行即可。
 //class 使用示例
 let _self = this;
 function updateUserName(value) {
     _self.setState({
         name: value
     });
 }
 let cleanUserNameHook = StoreApi.addStoreStateHook('userName', updateUserName); //this will return the clean method for unSubcribe，返回值为取消本次hook的方法，需要时执行即可。

 */

let StoreApi = {
    StoreInfo: {},
    setStateValue: function(stateName, value, dispatch) {
        return (this.StoreInfo[stateName].stateValue = dispatch(value));
    },
    initThisStateData: function(stateName) {
        if (!this.StoreInfo[stateName]) {
            this.StoreInfo[stateName] = {};
            this.StoreInfo[stateName].hookList = [];
        }
    },
    onlySetStateValue: function(stateName, value) {
        this.initThisStateData(stateName);
        this.StoreInfo[stateName].stateValue = value;
    },
    getStateValue: function(stateName) {
        this.initThisStateData(stateName);
        return this.StoreInfo[stateName].stateValue;
    },
    getStoreState: function(stateName) {
        return this.getStateValue(stateName);
    },
    // StoreStateHookList: [],
    addStoreStateHook: function(stateName, cb) {
        if (typeof cb === "function") {
            this.initThisStateData(stateName);
            if (this.StoreInfo[stateName].hookList.indexOf(cb) === -1) {
                this.StoreInfo[stateName].hookList.push(cb);
                return () => {
                    this.removeStoreStateHook(stateName, cb);
                };
            }
        }
    },
    updateStoreState: function(stateName, value, dispatch) {
        this.initThisStateData(stateName);
        dispatch =
            dispatch ||
            function(value) {
                return value;
            };
        value = this.setStateValue(stateName, value, dispatch);
        for (
            let i = this.StoreInfo[stateName].hookList.length - 1;
            i >= 0;
            i--
        ) {
            let item = this.StoreInfo[stateName].hookList[i];
            item(value);
        }
    },
    removeStoreStateHook: function(stateName, cb) {
        this.initThisStateData(stateName);
        let ps = this.StoreInfo[stateName].hookList.indexOf(cb);
        if (ps > -1) {
            this.StoreInfo[stateName].hookList.splice(ps, 1);
            this._checkStateHookList(stateName);
        }
    },
    _checkStateHookList: function(stateName) {
        if (this.StoreInfo[stateName].hookList.length === 0) {
            // this.cleanState(stateName);
        }
    },
    cleanState: function(stateName) {
        delete this.StoreInfo[stateName];
    },
    getStoreInfo: function() {
        return this.StoreInfo;
    },
    setStoreState: function(stateName, value, dispatch) {
        this.updateStoreState(stateName, value, dispatch);
    },
    validateState: function() {}
};

global.StoreApi = StoreApi;

export default StoreApi;
